package cn.worldyao.demo.controller;


import cn.worldyao.demo.entity.*;
import cn.worldyao.demo.service.SysPermissionRoleService;
import cn.worldyao.demo.service.SysPermissionService;
import cn.worldyao.demo.service.SysRoleService;
import cn.worldyao.demo.util.ReturnSet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yaozi
 * @since 2020-03-11
 */
@RequestMapping("/sys-role")
@RestController
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @CrossOrigin
    @PostMapping("/add")
    public ReturnSet addRole(@RequestBody SysPermissionRoleItem sysPermissionRoleItem) {

        return sysRoleService.save(sysPermissionRoleItem) ?
                ReturnSet.success("角色添加成功") :
                ReturnSet.fail("角色添加失败");
    }

    @GetMapping("list")
    public ReturnSet RoleList() {
        List<SysRole> sysRoles = sysRoleService.listRoles();
        JSONArray array = JSONArray.parseArray(JSON.toJSONString(sysRoles));
        System.out.println(array);
        return ReturnSet.success(array);
    }

    @CrossOrigin
    @PostMapping(value = "/updatePermission")
    public ReturnSet RolePermission(@RequestBody SysPermissionRoleItem sysPermissionRoleItem) {
        return sysRoleService.updatePermission(sysPermissionRoleItem) ?
                ReturnSet.success("权限更新成功") :
                ReturnSet.fail("权限更新失败");
    }
}
